//
//  taskDataSource.m
//  tcpoctest
//
//  Created by gump on 6/9/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "taskDataSource.h"
#import "TaskInfo.h"
#import "sendTaskComplete.h"

@implementation taskDataSource

-(id)init
{
    self = [super init];
    
    if (self != nil) {
        taskDic = [[NSMutableDictionary alloc] init];
        taskShowArr = [[NSMutableArray alloc] init];
        
        pthread_mutex_init(&mutexTask, 0);
        
        NSString* pathpath = [[NSBundle mainBundle] pathForResource:@"task" ofType:@"xml"];
        NSFileHandle* file = [NSFileHandle fileHandleForReadingAtPath:pathpath];
        NSData* data = [file readDataToEndOfFile];
        NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; //设置XML数据
        [parser setShouldProcessNamespaces:NO];
        [parser setShouldReportNamespacePrefixes:NO];
        [parser setShouldResolveExternalEntities:NO];
        
        [parser setDelegate:self];
        [parser parse];
    }
    
    return self;
}

#pragma mark -xml parser-
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    pthread_mutex_lock(&mutexTask);
    
    if ([elementName isEqualToString:@"task"])
    {
    TaskInfo *tinfo = [[TaskInfo alloc] init];
    tinfo.taskname = [attributeDict objectForKey:@"cname"];
    tinfo.strtaskid = [attributeDict objectForKey:@"id"];
    tinfo.strEngname = [attributeDict objectForKey:@"name"];
    tinfo.strmoney = [attributeDict objectForKey:@"money"];
    tinfo.strDesc = [attributeDict objectForKey:@"desc"];
    
    const char *char_content = [tinfo.strmoney cStringUsingEncoding:NSASCIIStringEncoding];
        
    tinfo.strDesc = [NSString stringWithFormat:tinfo.strDesc,char_content];
    
    if ([taskDic objectForKey:tinfo.strtaskid] == nil
        && ![taskShowArr containsObject:tinfo.strtaskid]) {
        [taskDic setObject:tinfo forKey:tinfo.strtaskid];
        [taskShowArr addObject:tinfo.strtaskid];
    }
    }
    
    pthread_mutex_unlock(&mutexTask);
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
}



- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
}

#pragma mark -private fun-

-(void)CompleteTaskByID:(int)iid withIsDirectShow:(bool)bshow
{
    pthread_mutex_lock(&mutexTask);
    
    NSString *strid = [NSString stringWithFormat:@"%d",iid];
    
    TaskInfo *tinof = [taskDic objectForKey:strid];
    if (tinof != nil &&
        tinof.itaskState == TASK_STATE_NOGET) {
        tinof.itaskState = TASK_STATE_GET;
        
        [[netManager sharedNetManager].sendTaskC sendTaskTheComplete:
         iid];
        
        self.strTaskText = [NSString stringWithFormat:@"恭喜您！%@",
                            tinof.strDesc];
        if (bshow) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:
                                  [DataControl shareControl].taskDS.strTaskText
                                                           delegate:self
                                                  cancelButtonTitle:@"确认"
                                                  otherButtonTitles:nil];
            [alert show];
        }
        else
        {
        [[DataControl shareControl].UICommendDS addUICommand:UC_TASK_COMPLETE];
        }
        
    }
    
    pthread_mutex_unlock(&mutexTask);
}

-(int)GetShowCount
{
    int icount = 0;
    
    pthread_mutex_lock(&mutexTask);
    
    icount = taskDic.count;
    
    pthread_mutex_unlock(&mutexTask);
    
    return icount;
}

-(TaskInfo *)getInfoByRow:(int)irow
{
    TaskInfo *tinfo = nil;
    
    pthread_mutex_lock(&mutexTask);
    
    if (irow >= 0 && irow < taskShowArr.count) {
        NSString *strid = [taskShowArr objectAtIndex:irow];
        tinfo = [taskDic objectForKey:strid];
    }
    
    pthread_mutex_unlock(&mutexTask);
    
    return tinfo;
}

-(void)ClearState
{
    pthread_mutex_lock(&mutexTask);
    
    for(NSString *compKey in taskDic) {    
        TaskInfo *comp = [taskDic objectForKey:compKey];
        if (comp != nil) {
            comp.itaskState = TASK_STATE_NOGET;
        }
    }
    
    pthread_mutex_unlock(&mutexTask);
}

-(void)setComplete:(int)iid
{
    pthread_mutex_lock(&mutexTask);
    
    NSString *strid = [NSString stringWithFormat:@"%d",iid];
    
    TaskInfo *tinof = [taskDic objectForKey:strid];
    if (tinof != nil &&
        tinof.itaskState == TASK_STATE_NOGET) {
        tinof.itaskState = TASK_STATE_GET;
        
        
        
    }
    
    pthread_mutex_unlock(&mutexTask);
}

@end
